<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人资料 - TechFolio 个人项目展示网站</title>
    <link rel="stylesheet" href="/css/element-ui.css">
    <link rel="stylesheet" href="/css/common.css">
    <script src="/js/vue.js"></script>
    <script src="/js/element-ui.js"></script>
    <script src="/js/axios.min.js"></script>
    <script src="/js/qs.js"></script>
</head>
<body>
<div id="app">
    <el-container>
        <!-- 头部 -->
        <el-header class="layout-header" style="">
            <div class="title">
                <h1>TechFolio 个人项目展示网站 v1.0.0</h1>
            </div>
            <el-breadcrumb separator-class="el-icon-arrow-right"
                           style="height: 48px; line-height: 48px; font-size: 16px;">
                <el-breadcrumb-item>后台管理</el-breadcrumb-item>
                <el-breadcrumb-item>个人资料</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <!-- 主体 -->
        <el-container class="layout-body">
            <!-- 左侧 -->
            <el-aside width="80px" class="layout-aside">
                <div class="menu-item">
                    <a href="profile.html" class="active">
                        <div><i class="el-icon-user"></i></div>
                        <div class="menu-item-text">个人资料</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a href="project.html">
                        <div><i class="el-icon-document-copy"></i></div>
                        <div class="menu-item-text">项目管理</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a href="security.html">
                        <div><i class="el-icon-lock"></i></div>
                        <div class="menu-item-text">账号安全</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a @click="logout">
                        <div><i class="el-icon-switch-button"></i></div>
                        <div class="menu-item-text">退出登录</div>
                    </a>
                </div>
            </el-aside>
            <!-- 右侧 -->
            <el-main class="layout-main">
                <!-- 操作区域 -->
                <div class="operate-area">
                    <h1>个人资料</h1>
                </div>
                <!-- 系统设置主体，分左右两个区域 -->
                <el-row :gutter="20">
                    <!-- 左侧区域：表单 -->
                    <el-col :span="16">
                        <!-- 基本资料 -->
                        <el-card>
                            <div slot="header" class="clearfix">
                                <span>基本资料</span>
                            </div>
                            <el-form :model="formProfile" label-width="100px">
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="姓名">
                                            <el-input v-model="formProfile.name"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="性别">
                                            <el-radio-group v-model="formProfile.gender">
                                                <el-radio :label="1">男</el-radio>
                                                <el-radio :label="0">女</el-radio>
                                            </el-radio-group>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="出生日期">
                                            <el-date-picker v-model="formProfile.dayOfBirth" type="date"
                                                            value-format="yyyy-MM-dd"
                                                            placeholder="选择日期"></el-date-picker>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="学历">
                                            <el-select v-model="formProfile.educationLevel" placeholder="请选择学历">
                                                <el-option label="初中" value="初中"></el-option>
                                                <el-option label="高中或中专" value="高中或中专"></el-option>
                                                <el-option label="大学专科" value="大学专科"></el-option>
                                                <el-option label="大学本科" value="大学本科"></el-option>
                                                <el-option label="硕士研究生" value="硕士研究生"></el-option>
                                                <el-option label="博士研究生" value="博士研究生"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="联系电话">
                                            <el-input v-model="formProfile.phoneNumber"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="电子邮箱">
                                            <el-input v-model="formProfile.email"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="毕业院校">
                                            <el-input v-model="formProfile.graduationSchool"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="专业">
                                            <el-input v-model="formProfile.major"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-form-item label="自我介绍">
                                    <el-input type="textarea" v-model="formProfile.description" :rows="8"></el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button @click="resetForm">重置</el-button>
                                    <el-button type="primary" @click="updateProfile">保存</el-button>
                                </el-form-item>
                            </el-form>
                        </el-card>
                    </el-col>
                    <!-- 右侧区域：照片 -->
                    <el-col :span="8">
                        <el-card>
                            <div slot="header" class="clearfix">
                                <span>照片</span>
                            </div>
                            <div>
                                <div><img :src="formProfile.photoRemoteUrl" style="width: 100%; aspect-ratio: 3/4;">
                                </div>
                                <div style="text-align: center; margin-top: 10px;">
                                    <el-button size="small" @click="uploadDialog.visible = true">更换照片</el-button>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <!-- 弹出对话框：更换照片 -->
                <el-dialog title="更换照片" :visible.sync="uploadDialog.visible" width="35%" @open="clearPreview">
                    <el-upload
                            class="upload-demo"
                            action=""
                            :show-file-list="false"
                            :before-upload="beforeUpload"
                            :on-change="handleUploadChange">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                    <div>
                        <img v-if="uploadDialog.imageUrl" :src="uploadDialog.imageUrl"
                             style="width: 100%; height: 350px; object-fit: contain; margin-top: 10px;">
                    </div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="uploadDialog.visible = false">取消</el-button>
                        <el-button type="primary" @click="submitUpload">提交</el-button>
                    </div>
                </el-dialog>
            </el-main>
        </el-container>
    </el-container>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                uploadDialog: {
                    visible: false,
                    imageUrl: '',
                    file: null,
                },
                formProfile: {
                    name: '',
                    gender: 1,
                    dayOfBirth: '',
                    photoRemoteUrl: '',
                    phoneNumber: '',
                    email: '',
                    graduationSchool: '',
                    major: '',
                    educationLevel: '',
                    description: '',
                },
                originalProfile: {}
            };
        },
        methods: {
            clearPreview() {
                this.uploadDialog.imageUrl = '';
                this.uploadDialog.file = null;
            },
            handleUploadChange(file) {
                this.uploadDialog.file = file;
                this.uploadDialog.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                const isLt10M = file.size / 1024 / 1024 < 10;

                if (!isJPG && !isPNG) {
                    this.$message.error('照片只能是 JPG/PNG 格式!');
                }
                if (!isLt10M) {
                    this.$message.error('照片大小不能超过 10MB!');
                }
                return (isJPG || isPNG) && isLt10M;
            },
            submitUpload() {
                if (!this.uploadDialog.file) {
                    this.$message.error('请先选择图片');
                    return;
                }
                let url = '/api/image/upload';

                let formData = new FormData();
                formData.append('file', this.uploadDialog.file.raw);
                formData.append('imageType', 0);

                axios.post(url, formData, {headers: {'Content-Type': 'multipart/form-data'}}).then(response => {
                    let jsonResult = response.data;
                    if (jsonResult.state === 20000) {
                        this.formProfile.photoImageId = jsonResult.data.id;
                        this.formProfile.photoRemoteUrl = jsonResult.data.remoteUrl;
                        this.uploadDialog.visible = false;
                        this.uploadDialog.imageUrl = '';
                        this.$alert('上传照片成功，请点击“保存”按钮进行保存，否则照片不会被保存！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    } else if (jsonResult.state === 40100) {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                                window.location.href = 'login.html';
                            }
                        });
                    } else {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    }
                }).catch(error => {
                    this.$alert('服务器端无法正确响应【上传照片】功能！', '错误', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                });
            },
            fetchProfile() {
                let url = '/api/profile';
                axios.get(url).then(response => {
                    let jsonResult = response.data;
                    if (response.data.state === 20000) {
                        this.formProfile = response.data.data;
                        this.originalProfile = {...this.formProfile};
                    } else if (jsonResult.state === 40100) {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                                window.location.href = 'login.html';
                            }
                        });
                    } else {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    }
                }).catch(error => {
                    this.$alert('服务器端无法正确响应【获取个人信息】功能！', '错误', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                });
            },
            resetForm() {
                this.formProfile = {...this.originalProfile};
            },
            updateProfile() {
                let url = '/api/profile/update';
                let params = Qs.stringify(this.formProfile);
                axios.post(url, params).then(response => {
                    let jsonResult = response.data;
                    if (jsonResult.state === 20000) {
                        this.$message.success('修改成功');
                    } else if (jsonResult.state === 40100) {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                                window.location.href = 'login.html';
                            }
                        });
                    } else {
                        this.$alert(jsonResult.message, '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    }
                }).catch(error => {
                    this.$alert('服务器端无法正确响应【更新个人资料】功能！', '错误', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                });
            },
            // 退出登录
            logout() {
                let message = '登录信息将被注销，在重新登录之前不可进行后台管理操作，是否继续？';
                this.$confirm(message, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = '/api/admin/logout';
                    axios.post(url).then(response => {
                        let jsonResult = response.data;
                        if (jsonResult.state === 20000) {
                            window.location.href = '/';
                        } else {
                            this.$alert(jsonResult.message, '错误', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    window.location.href = 'login.html';
                                }
                            });
                        }
                    }).catch(error => {
                        this.$alert('服务器端无法正确响应【退出登录】功能！', '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    });
                }).catch(() => {
                });
            }
        },
        mounted() {
            this.fetchProfile();
        }
    });
</script>
</body>
</html>